﻿#region Copyright (c) 2011 R. Uittenbosch
/*
* Copyright (C) 2011 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseViewer.Interfaces;

using System.Data.EntityClient;
using University.Db;


namespace UniversityViewer
{
    /// <summary>
    /// Example database model
    /// </summary>
    public class University : IDbModel
    {
        private const string _filterName = "Student";
        /// <summary>
        /// The connectionstring hosted in de configurationfile on which to perform the lookup.
        /// </summary>
        public string ConnectionString { set; private get; }

        /// <summary>
        /// Create the actual tree of object(types) that need to be visible.
        /// </summary>
        public void CreateTree()
        {
            //
            //
            //
            //                                             CourseSet -> DepartmentSet
            // StudentSet -> EnrollmentSet -> SectionSet <
            //                                             LocationSet
            //
            //
            Tree =
                Factory.CreateNode<StudentSet>(ControlType.Container1,
                        Factory.CreateNode<EnrollmentSet>(ControlType.Container2,
                                Factory.CreateNode<SectionSet>(
                                        Factory.CreateNode<CourseSet>(
                                                Factory.CreateNode<DepartmentSet>()),
                                        Factory.CreateNode<LocationSet>())));            
        }

        /// <summary>
        /// The factory needed to create nodes.
        /// </summary>
        public IVisualizerNodeFactory Factory {set; private get; }

        /// <summary>
        /// In the UI we can filter on two items, StudentId en StudentName
        /// </summary>
        public List<string> FilterProperties
        {
            get { return new List<string>() { "StudentId", "StudentName" }; }
        }

        /// <summary>
        /// Create a Filter from the controllist.
        /// </summary>
        /// <param name="controls">A list of control values and their corresponding names.</param>
        /// <returns></returns>
        public IFilter LoadFromControl(IControlList controls)
        {
            // Create the filter object
            var filter = Factory.CreateFilter();
            // Was anything filled in the StudentId control? 
            var id = controls.GetControlValue("StudentId");
            
            if (!string.IsNullOrEmpty(id))
                // Just add it, we are going to filter on this object
                filter.Add(_filterName, id);
            else
            {
                // Was anything filled in the StudentName control?
                var name = controls.GetControlValue("StudentName");
                if (!string.IsNullOrEmpty(name))
                {                   
                    // Because we only support one filter in our LoadTree method, we have to retrieve
                    // the id of the student with the given name from the database
                    UniversityDataContext context = new UniversityDataContext(ConnectionString);
                    StudentSet student = context.StudentSets.Where(s => s.LastName == name).FirstOrDefault();
                    // If found, add it's id to our filterobject.
                    if (student != null)
                        filter.Add(_filterName, student.Id.ToString());
                    
                }
            }            
            
            return filter;
        }

        /// <summary>
        /// Load the tree object with the data of our database
        /// </summary>
        /// <param name="filter"></param>
        public void LoadTree(IFilter filter)
        {         
            // Load the student with the given id from the database
            UniversityDataContext context = new UniversityDataContext(ConnectionString);
            int id = Int32.Parse(filter.Get(_filterName));
            StudentSet student = context.StudentSets.Where(s => s.Id == id).FirstOrDefault();

            // Fill the datasource
            Tree.DataSource = new List<StudentSet>() { student };

            // Adding datasources that for some reason can not be linked by ForeignKeys.
            // Just here as an example.


            // Tree.FindNode<EnrollmentSet>().DataSource = student.EnrollmentSets;
        }

        /// <summary>
        /// The actual tree that we want to see
        /// </summary>
        public IVisualizerNode Tree { get; private set;}
    }
}
